package com.face.server.repository.exRepository;

import com.face.server.entity.DormitoryAdmin;
import com.face.server.service.UtileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by ryan on 2018/1/26.
 */
@Repository
public class exDormAdminRepositoryImpl implements exDormAdminRepository {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<DormitoryAdmin> search(Pageable pageRequest, String key1, String key2) {
        Criteria integ=new Criteria();
        List<Criteria> temp = new ArrayList<>();
        temp.add(new Criteria("type").is(2));

        String[] keys=new String[]{"name","phone","idNumber","jobName"};
        List<Criteria> criteriaList=new ArrayList<Criteria>();
        Criteria criteria=new Criteria();
        Criteria criteria1=new Criteria();
        if (key1!=null)
            criteria1.orOperator(Arrays.stream(keys).map(e->Criteria.where(e).regex(key1)).toArray(s->new Criteria[s]));
        Criteria criteria2=new Criteria();
        if (key1!=null&& UtileService.isNum(key1))
            criteria2.and("dormitoryBuildNum").is(Integer.parseInt(key1));
        criteriaList.add(criteria1);criteriaList.add(criteria2);
        criteria.orOperator(criteriaList.stream().toArray(s->new Criteria[s]));
        temp.add(criteria);

        if (key2!=null&&key2.equals("")==false){
            List<Criteria> criList=new ArrayList<Criteria>();
            Criteria cri=new Criteria();
            Criteria cri1=new Criteria();
            cri1.orOperator(Arrays.stream(keys).map(e->Criteria.where(e).regex(key2)).toArray(s->new Criteria[s]));
            Criteria cri2=new Criteria();
            if (UtileService.isNum(key2))
                cri2.and("dormitoryBuildNum").is(Integer.parseInt(key2));
            criList.add(cri1);
            criList.add(cri2);
            cri.orOperator(criList.stream().toArray(s->new Criteria[s]));
            temp.add(cri);
        }
        integ.andOperator(temp.stream().toArray(s->new Criteria[s]));
//        List<DormitoryAdmin> dormitoryAdminList=mongoTemplate.find(Query.query(criteria).with(pageRequest),DormitoryAdmin.class);
//        List<DormitoryAdmin> dormitoryAdminList1=dormAdminRepository.findByNameRegexOrPhoneRegexOrIdNumberRegexOrAndDormitoryNumListIs(key1,key1,key1,Integer.parseInt(key1),pageRequest).getContent();
        int page=pageRequest.getPageNumber()>0?pageRequest.getPageNumber()*pageRequest.getPageSize():0;
        Aggregation aggregation=Aggregation.newAggregation(
//                Aggregation.unwind("dormitoryNumList"),
                Aggregation.match(integ),
                Aggregation.sort(pageRequest.getSort()),
                Aggregation.skip((long)page),
                Aggregation.limit(pageRequest.getPageSize())
        );
        AggregationResults<DormitoryAdmin> aggrResult = mongoTemplate.aggregate(aggregation, "joined", DormitoryAdmin.class);
        List<DormitoryAdmin> dormitoryAdmins=new ArrayList<DormitoryAdmin>();
        aggrResult.getMappedResults().forEach(e->dormitoryAdmins.add((e)));
        return dormitoryAdmins;
    }

    @Override
    public List<DormitoryAdmin> search(Pageable pageRequest, String key1, String key2, List<Integer> dormitoryNumList) {
        Criteria integ=new Criteria();
        List<Criteria> temp = new ArrayList<>();
        temp.add(new Criteria("type").is(2));

        Criteria criBuild=new Criteria();
        criBuild.and("dormitoryBuildNum").in(dormitoryNumList);

        String[] keys=new String[]{"name","phone","idNumber","jobName"};
        List<Criteria> criteriaList=new ArrayList<Criteria>();
        Criteria criteria=new Criteria();
        Criteria criteria1=new Criteria();
        if (key1!=null)
            criteria1.orOperator(Arrays.stream(keys).map(e->Criteria.where(e).regex(key1)).toArray(s->new Criteria[s]));
        Criteria criteria2=new Criteria();
        if (key1!=null&& UtileService.isNum(key1))
            criteria2.and("dormitoryBuildNum").is(Integer.parseInt(key1));
        criteriaList.add(criteria1);criteriaList.add(criteria2);
        criteria.orOperator(criteriaList.stream().toArray(s->new Criteria[s]));
        temp.add(criteria);

        if (key2!=null&&key2.equals("")==false){
            List<Criteria> criList=new ArrayList<Criteria>();
            Criteria cri=new Criteria();
            Criteria cri1=new Criteria();
            cri1.orOperator(Arrays.stream(keys).map(e->Criteria.where(e).regex(key2)).toArray(s->new Criteria[s]));
            Criteria cri2=new Criteria();
            if (UtileService.isNum(key2))
                cri2.and("dormitoryBuildNum").is(Integer.parseInt(key2));
            criList.add(cri1);
            criList.add(cri2);
            cri.orOperator(criList.stream().toArray(s->new Criteria[s]));
            temp.add(cri);
        }
        integ.andOperator(temp.stream().toArray(s->new Criteria[s]));
//        List<DormitoryAdmin> dormitoryAdminList=mongoTemplate.find(Query.query(criteria).with(pageRequest),DormitoryAdmin.class);
//        List<DormitoryAdmin> dormitoryAdminList1=dormAdminRepository.findByNameRegexOrPhoneRegexOrIdNumberRegexOrAndDormitoryNumListIs(key1,key1,key1,Integer.parseInt(key1),pageRequest).getContent();
        int page=pageRequest.getPageNumber()>0?pageRequest.getPageNumber()*pageRequest.getPageSize():0;
        Aggregation aggregation=Aggregation.newAggregation(
                Aggregation.match(integ),
                Aggregation.sort(pageRequest.getSort()),
                Aggregation.skip((long)page),
                Aggregation.limit(pageRequest.getPageSize())
        );
        AggregationResults<DormitoryAdmin> aggrResult = mongoTemplate.aggregate(aggregation, "joined", DormitoryAdmin.class);
        List<DormitoryAdmin> dormitoryAdmins=new ArrayList<DormitoryAdmin>();
        aggrResult.getMappedResults().forEach(e->dormitoryAdmins.add((e)));
        return dormitoryAdmins;
    }
}
